import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import java.lang.Thread;
import java.lang.Runnable;
import java.util.Random;
import javax.microedition.lcdui.Image;
public class GameBoard extends Canvas implements Runnable{
	private PlayMenu playmenu;	
	private Display display;
	private Thread waittime;
	private Random random;
	private boolean fishing, run, alreadywait, miss1, catched, miss2, err;
	private int sleeptime, nextimage, imagedelay, numfish, checkdelay, keystate, timebeforefishing;
	private Image[] num, drawimage;
	private int[] numimage;
	private String keyname;
	public GameBoard(PlayMenu playmenu, Display display){
		this.playmenu = playmenu;
		this.display = display;
		waittime = new Thread(this);
		waittime.start();
		random = new Random();		
		fishing = false;
		alreadywait = false;
		catched = false;
		keystate = 0;
		miss1 = false;
		miss2 = false;
		err = false;
		sleeptime = 0;
		nextimage = 0;	
		checkdelay = 10;	
		timebeforefishing = 0;
		keyname = "";
		//imagedelay dua vao Level cua nguoi choi de tinh la delay nhanh hay cham, o day chua tinh toi
		imagedelay = 1000;	
		run = true;		
		//drawimage la day cac hinh da duoc sap xep va se duoc ve, kich thuoc cua mang nay la tong so cac hinh se duoc ve, so nay co the tang len theo Level
		//drawimage = new Image[random.nextInt(9)]; // so 9 nay la tong so hinh se duoc ve, so nay se duoc tang len, neu so nay la 9 thi tong so hinh se la 10 vi so hinh luon >=1		
		num = new Image[10];
		//numimage = new int[drawimage.length];
		try{
			num[1] = Image.createImage("/num/1.png");
			num[2] = Image.createImage("/num/2.png");
			num[3] = Image.createImage("/num/3.png");
			num[4] = Image.createImage("/num/4.png");
			num[5] = Image.createImage("/num/5.png");
			num[6] = Image.createImage("/num/6.png");
			num[7] = Image.createImage("/num/7.png");
			num[8] = Image.createImage("/num/8.png");
			num[9] = Image.createImage("/num/9.png");
			num[0] = Image.createImage("/num/0.png");
		}
		catch(Exception e){}	
		setFullScreenMode(true);
	}
	public void run(){
		while(run){
			if(miss1 == true){
				fishing = false;
				alreadywait = false;
				fishing = false;
				catched = false;
				nextimage = 0;
				imagedelay = 1000;
				sleeptime = 0;	
			}
			if(miss2 == true){
				fishing = false;
				alreadywait = false;
				fishing = false;
				catched = false;
				nextimage = 0;
				imagedelay = 1000;
				sleeptime = 0;	
			}
			if((fishing == true)&&(miss1 == false)){				
				display.flashBacklight(1000);				
				while(timebeforefishing != 100){
					timebeforefishing++;	
					repaint();	
				}				
				try{				
					//waittime.sleep(sleeptime);
					waittime.sleep(5000);					
				}
				catch(Exception e){}
				if(miss1 == false)
					display.flashBacklight(1000);					
				fishing = false;	
				alreadywait = true;	
				setImageArray();					
			}
			if(keystate == 1){
				checkdelay --;
				if(checkdelay == 0){	
					keystate = 0;
					checkdelay = 10;
					imagedelay = 1000;
					nextimage++;
				}
			}
			if((fishing == false)&&(alreadywait == true)&&(miss1 == false)&&(keystate == 0)){		
				imagedelay --;	
				if(imagedelay == 0){
					miss2 = true;
					err = true;
				}
				if(nextimage == drawimage.length)
					catched = true;	
					numfish = random.nextInt(500);	
			}		
			if(catched == true){
				alreadywait = false;
				fishing = false;				
				nextimage = 0;
				imagedelay = 1000;
				sleeptime = 0;		
				if(Option.vibration == true)
				try{
					display.vibrate(random.nextInt(10)*100);
					waittime.sleep(700);
				}
				catch(Exception e){}
			}
		repaint();
		}
	}
	public void paint(Graphics g){
		g.setColor(0,0,0);
		g.fillRect(0,0,getWidth(), getHeight());
		g.setColor(255,255,255);
		g.fillRect(0,0,128,160);
		//Neu le ve hinh thi khoi can 3 cau duoi	
		//g.setColor(35,55,235);
		//g.fillRect(0,0,128,160);		
		g.setColor(0,0,0);
		if(Option.language == true){
			if(miss1 == true){			
				g.drawString("Ban da giat can ", 28, 65, 0);
				g.drawString("truoc khi ca can cau !", 13, 80,0);
				g.drawString("Ban da bi mat 1 moi cau !", 2, 110,0);
				g.drawString("Quang Cau", 2, 145, 0);
				g.drawString("Tro ve", 95, 145, 0);
			}
			if(miss2 == true){			
				g.drawString("TRAT ROI !", 35, 40, 0);
				g.drawString("Co the ban da bo lo", 15, 70,0);
				g.drawString("mot con ca lon !", 25, 85,0);
				g.drawString("Ban da bi mat 1 moi cau !", 2, 100,0);
				g.drawString("Quang Cau", 2, 145, 0);
				g.drawString("Tro ve", 95, 145, 0);
			}
			if((fishing == true)&&(alreadywait == false)&&(miss1 == false)){	
				if(timebeforefishing < 100){							
					//Thuc ra thi ve hinh khac len man hinh thi hay hon
					g.setColor(255,255,255);
					g.fillRect(0,0,128,160);
					g.setColor(0,0,0);								
					g.drawString("Dang cau ca", 35, 65,0);
					g.drawString("Dung", 100, 145,0);	
				}
				else{
					g.setColor(0,0,0);
					g.fillRect(0,0,128,160);
				}				
				
			}	
			else if((fishing ==false)&&(alreadywait == false)){				
				g.drawString("Quang Cau", 2, 145, 0);
				g.drawString("Tro ve", 95, 145, 0);
			}	
			if((fishing == false)&&(alreadywait == true)&&(catched == false)&&(keystate == 0)){
				g.setColor(255,255,255);
				g.fillRect(0,0,128,160);	
				g.setColor(255,255,255);
				g.fillRect(0,0,128,160);	
				g.drawImage(drawimage[nextimage],54,0,0);
				g.setColor(0,0,0);
				g.drawString("Dung", 100, 145,0);	
			}	
			if((fishing == false)&&(alreadywait == true)&&(catched == false)&&(keystate == 1)){				
				g.setColor(0,0,0);
				g.drawString("Dung",55,55,0);
			}	
			if(catched == true){
				g.drawString("DINH ROI !",38,40,0);
				g.drawString("Chuc mung ban da", 17,70,0);
				g.drawString("cau duoc loai ca "+ numfish + " !", 11,85,0);
				g.drawString("Ban da mat 1 moi cau !", 9, 100,0);
				g.drawString("Quang Cau", 2, 145, 0);
				g.drawString("Tro ve", 95, 145, 0);
			}
			//g.setColor(0,0,0);
			//g.drawString(Integer.toString(checkdelay), 5,30,0);
			g.drawString(Integer.toString(imagedelay), 5, 15,0);	
			if(err == true){
				g.setColor(0,0,0);
				g.drawString(keyname, 5,45,0);
			}
			//g.drawString(keyname, 5,45,0);
			//g.drawString(Integer.toString(nextimage), 110,15,0);
			//g.drawString(Integer.toString(drawimage.length), 110,30,0);
		}
		else{
			if(miss1 == true){
				g.drawString("Ban da giat can ", 25, 65, 0);
				g.drawString("truoc khi ca can cau", 15, 80,0);
				g.drawString("Ban da bi mat 1 moi cau", 7, 110,0);
				fishing = false;
			}
			if((fishing == true)&&(alreadywait == false)){
				//Thuc ra thi ve hinh khac len man hinh thi hay hon
				g.drawString("Fishing", 50, 80, 0);
				g.drawString("Stop", 105, 145,0);
			}
			else if((fishing ==false)&&(alreadywait == false)){
				g.drawString("Throw Rod", 2, 145, 0);
				g.drawString("Back", 103, 145, 0);
			}			
		}
		//g.drawString(Integer.toString(sleeptime), 20, 95, 0);
		
	}
	protected void keyPressed(int keycode){		
		if(keycode == -7){			
			if(fishing == true){
				fishing = false;				
				miss1 = true;
				sleeptime = 0;		
			}
			else if((fishing == false)&&(alreadywait == true)){
				alreadywait = false;
				miss2 = true;	
			}
			else if((fishing == false)&&(alreadywait == false)){
				catched = false;
				fishing = false;				
				miss1 = false;
				miss2 = false;
				keystate = 0;
				alreadywait = false;
				display.setCurrent(playmenu);
			}				
		}
		if(keycode == -6){
			if(fishing == true){
				fishing = false;			
				miss1 = true;
				sleeptime = 0;		
			}
			else if(fishing == false){				
				catched = false;
				fishing = true;				
				miss1 = false;
				miss2 = false;
				keystate = 0;
				timebeforefishing = 0;
				keycode  = 0;
				alreadywait = false;
				try{
					sleeptime = random.nextInt(150)*1000+30000;				
				}
				catch(Exception e){}
			}
		}
		if((keycode >34)&&(fishing == true)){
			miss1 = true;
			sleeptime = 0;
		}
		if((keycode > 34)&&(alreadywait == true)&&(keystate == 0)){
			keyname = getKeyName(keycode);
			checkKey(keycode);
		}
		repaint();
	}
	public void setImageArray(){
		drawimage = new Image[random.nextInt(9)+5];
		numimage = new int[drawimage.length];
		int randomimage;
		for(int i = 0; i < drawimage.length; i ++){
			randomimage = random.nextInt(9);
			drawimage[i] = num[randomimage];
			numimage[i] = randomimage;
		}
	}
	public void checkKey(int keycode){
		if((numimage[nextimage] + 48) == keycode){
			keystate = 1;
		}
		else{
			keystate = 2;
			miss2 = true;
		}		
	}
}